<template>
  <section>
    <ue-search-bar :data="querys" ref="ueSearchBar" v-model="searchForm">
    </ue-search-bar>
    <el-col :span="24">
      <el-button type="primary"  class="mButtonRight mButton" @click.native="resetSearchForm">重置</el-button>
      <el-button type="primary" class="mButtonRight mButton" @click.native="searchMessageRecords">查询</el-button>
    </el-col>
    <el-table :data="tableData" highlight-current-row border max-height="680">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="sendTime" label="发送时间"></el-table-column>
      <el-table-column prop="name" label="消息名称"></el-table-column>
      <el-table-column prop="title" label="消息标题"></el-table-column>
      <el-table-column prop="content" label="消息内容"></el-table-column>
      <el-table-column prop="target" label="目标客户">
        <template slot-scope="scope">
          <label v-if="scope.row.target === 'ALL'">所有用户</label>
          <label v-if="scope.row.target === 'PERSON_GROUP'">用户分群</label>
          <label v-if="scope.row.target === 'PERSONAL_GRADE'">会员等级</label>
        </template>
      </el-table-column>
      <el-table-column  label="成功/失败(条)">
        <template slot-scope="scope">{{scope.row.success}}/{{scope.row.fail}}</template>
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button type="primary" @click.native="personalGroups(scope.row)">推送对象</el-button>
          <el-button type="danger" @click.native="deletMessage(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-row>
      <el-col :span="24" class="toolbar">
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total,sizes,prev, pager, next, jumper"
          :total="total" style="float: right;">
        </el-pagination>
      </el-col>
    </el-row>
    <message-personal-group ref="messagePersonalGroup"></message-personal-group>
  </section>
</template>

<script>
import {getAllMessages, deleteMessage} from '../../api/v3/Message';
import MessagePersonalGroup from './MessagePersonalGroup.vue';
export default {
  components: {MessagePersonalGroup},
  data () {
    return {
      searchForm: {},
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    };
  },
  methods: {
    getTableData (filters) {
      getAllMessages({
        filters: filters,
        sort: 'createdDate,desc',
        page: this.currentPage - 1,
        size: this.pageSize
      }).then(res => {
        this.tableData = res.data;
        this.total = Number(res.headers['x-total-count']);
      });
    },
    searchMessageRecords () {
      let filters = '' + this.$ue.objToFilter(this.searchForm);
      this.getTableData(filters);
    },
    deletMessage (val) {
      this.$confirm('确认删除该记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        deleteMessage(val).then((res) => {
          this.$message.success('删除成功');
          this.searchMessageRecords();
        });
      });
    },
    personalGroups (val) {
      this.$refs.messagePersonalGroup.show(val);
    },
    resetSearchForm () {
      this.$refs.ueSearchBar.reset();
    },
    handleCurrentChange (val) {
      this.currentPage = val;
      this.searchMessageRecords();
    },
    handleSizeChange (val) {
      this.pageSize = val;
      this.searchMessageRecords();
    }
  },
  computed: {
    btns () {
      return this.$store.state.btns[this.$route.path] || [];
    },
    querys () {
      return this.$store.state.querys[this.$route.path + '#message'] || [];
    }
  },
  // 初始化数据
  mounted () {
    this.$store.commit('initDict');
    this.getTableData('');
  }
};
</script>

<style lang="less">
  .mButton {
    margin-bottom: 10px;
  }

  .mButtonRight {
    float: right;
    margin-right: 10px;
  }
</style>
